﻿using DAL;
using FubiManger.Controls;
using FubiManger.Pad;
using FubiManger.UI;
using Model;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;

namespace FubiManger.Cashier
{
    ////ToolboxItem用于控制是否添加自定义控件到工具箱，true添加，false不添加
    //[System.ComponentModel.ToolboxItem(true)]
    partial class cashordercontrol : Smobiler.Core.Controls.MobileUserControl
    {
        public cashordercontrol() : base()
        {
            //This call is required by the SmobilerUserControl.
            InitializeComponent();
        }
        /// <summary>
        /// 订单号
        /// </summary>
        public string ordsn { get; set; }
        private ms_order order = new ms_order();
        List<Gdpro> prolist = new List<Gdpro>();
        private void btnclose_Press(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 取消订单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnclare_Press(object sender, EventArgs e)
        {
            Pmessagecontrol con = new Pmessagecontrol();
            con.title = "订单提示";
            con.des = "是否确认取消订单，取消订单将不可恢复";
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (con.flag == true)
                {
                    if (orderDal.Claerordsn(ordsn) > 0)
                    {
                        order_markDal.Delbyordsn(ordsn);
                    }
                }
            });
        }
        /// <summary>
        /// 结算
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnsettlement_Press(object sender, EventArgs e)
        {
            if (order_markDal.Addrecord(order.order_sn, order.goods_info, order.merchant_id, order.pay_price) > 0)
            {
                btnsettlement.Enabled = false;
            }
        }
        /// <summary>
        /// 已收款
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnend_Press(object sender, EventArgs e)
        {
            Pmessagecontrol con = new Pmessagecontrol();
            con.title = "收款提示";
            con.des = "是否确定已收款";
            this.Form.ShowDialog(con, (obj, args) =>
            {
                if (con.flag == true)
                {
                    orderDal.Updatecodestatus(ordsn);
                    order_markDal.Delbyordsn(ordsn);
                    btnend.Enabled = false;
                    btnclare.Enabled = false;
                    this.Close();
                }
            });
        }

        private void cashordercontrol_Load(object sender, EventArgs e)
        {
            Getorder();
        }
        /// <summary>
        /// 获取订单信息
        /// </summary>
        private void Getorder()
        {
            order = orderDal.Querybysn(ordsn);
            if (order.user_tel.Length == 11)
                lbltel.Text = order.user_tel.Substring(0, 3) + "****" + order.user_tel.Substring(7, 4);
            else
                lbltel.Text = "现场下单";
            if (!string.IsNullOrEmpty(order.orderdisksn))
            {
                lbldisksn.Text = "餐桌号：" + order.orderdisksn;//餐桌号
                lbldisksn.Visible = true;
            }
            lblordsn.Text = order.order_sn;//订单编号
            lbltime.Text = Convert.ToDateTime(order.add_time).ToString("yyyy-MM-dd HH:mm:ss");
            lblprice.Text = "￥" + order.pay_price;//实付金额           
            if (order.orderremark.Length > 3)
                lblremark.Text = order.orderremark;//备注
            else
            {
                pantp.Height = 98;
                lblremark.Visible = false;

            }
            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
            prolist = js.Deserialize<List<Gdpro>>(order.goods_info);
            panpros.Height = prolist.Count * 44;
            panord.Height = panpros.Height + 30;
            panbtm.Location = new System.Drawing.Point(0, panord.Height + 100);
            pdesc.Height = pantp.Height + panord.Height + panbtm.Height + 8;
            panmiddle.Height = pdesc.Height + 25;
            //panall.Height = panord.Height + pantp.Height + 40;
            for (int i = 0; i < prolist.Count; i++)
            {
                ordergpcontrol con = new ordergpcontrol();
                con.proname = prolist[i].proname;
                con.cout = "x " + prolist[i].count;
                con.price = "￥ " + prolist[i].price;
                panpros.Controls.Add(con);
            }
        }

        /// <summary>
        /// 抹零
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Press(object sender, EventArgs e)
        {
            double paymoney = Convert.ToDouble(order.pay_price);
            int money = (int)paymoney;
            order.pay_price = money.ToString();
            lblprice.Text = "￥" + order.pay_price;//实付金额     
        }

        private void btnadd_Press(object sender, EventArgs e)
        {
            AddfoodForm frm = new AddfoodForm();
            frm.merchantid = order.merchant_id;//商户ID
            frm.carlist = prolist;//购物车商品
            frm.ordersn = ordsn;//订单号
            this.Form.Show(frm, true, (obj, args) =>
            {
                panpros.Controls.Clear();
                Getorder();//更新订单信息
            });
        }
    }
}